<el-dialog id="formGrant" title="授权管理" :visible.sync="dialogGrantVisible" width="800px" append-to-body v-cloak>
    <el-row :gutter="0" justify="start" align="top">
        <el-col :span="12">
            <span>选择用户</span>
            <el-input v-model="searchUser"
                      :disabled="false"
                      :style="{width:  '100%'}"
                      :clearable="true"
                      placeholder="搜索用户"
                      @input="searchUserChange()"
                      style="margin-top: 10px">
            </el-input>
            <el-table ref="multipleTable2"
                      border
                      :data="grantDataList"
                      row-key="id"
                      v-loading="loadingDialog"
                      tooltip-effect="dark"
                      :show-header="false"
                      max-height="200"
                      @selection-change="handleSelectionChangeGrant">
                <template slot="empty">{{emptyTextDialog}}</template>
                <el-table-column type="selection" width="40"></el-table-column>
                <el-table-column label="用户"  prop="managerName" show-overflow-tooltip>
                </el-table-column>
            </el-table>
            <#--<el-pagination-->
            <#--background-->
            <#--layout="total, prev, pager, next, jumper"-->
            <#--:current-page="currentPageDialog"-->
            <#--:page-size="pageSizeDialog"-->
            <#--:total="totalDialog"-->
            <#--class="ms-pagination"-->
            <#--@current-change='currentChangeGrant'>-->
            <#--</el-pagination>-->
        </el-col>
        <el-col :span="11" style="margin-left: 20px;">
            <span>权限</span>
            <el-tree
                    ref="treeGrant"
                    :data="dataTreeGrant"
                    show-checkbox
                    node-key="type"
                    :props="defaultPropsGrant"
                    :expand-on-click-node="false"
                    default-expand-all
                    style="margin-top: 10px;max-height: 240px;overflow-x: auto;"
                    v-loading="loadingGrant">
            </el-tree>
        </el-col>
    </el-row>
    <div slot="footer">
        <el-button size="mini" @click="dialogGrantVisible = false">取 消</el-button>
        <el-button size="mini" @click="resetGrant">重 置</el-button>
        <el-button size="mini" type="primary" @click="grantConfirm()">确 认</el-button>
    </div>
</el-dialog>
<script>
    var formGrant = new Vue({
        el: '#formGrant',
        data: function () {
            return {
                totalDialog: 0,
                //总记录数量
                pageSizeDialog: 10,
                //页面数量
                currentPageDialog: 1,
                //加载状态
                loadingDialog: true,
                emptyTextDialog:'',

                grantDataList:[],
                grantFrom:{},
                dialogGrantVisible:false,
                //授权用户选中
                selectionGrantList: [],
                searchUser:'',
                loadingGrant:false,
                defaultPropsGrant: {
                    children: 'children',
                    label: 'name'
                },
                dataTreeGrant: [],
            };
        },
        watch: {
        },
        computed: {},
        methods: {
            showGrantDialog:function (row) {
                this.initDialogData();
                this.grantFrom = {fileId:row[0].id}
                this.searchUser = ""
                this.dialogGrantVisible = true
                this.listGrant();
                this.getGrantPermission(row[0].id);
            },
            listGrant: function () {
                var that = this;
                this.loadingDialog = true;
                var page = {
                    pageNo: that.currentPageDialog,
                    pageSize: "99999"//that.pageSizeDialog
                };
                var form = {
                    managerNickname:this.searchUser
                }
                history.replaceState({
                    form: form,
                    page: page
                }, "");
                ms.http.post(ms.manager + "/basic/manager/search.do", Object.assign({}, form , page),{
                    headers: {
                        'Content-Type': 'application/json'
                    }
                }).then(function (res) {
                    that.loadingDialog = false;
                    if (!res.result || res.data.total <= 0) {
                        that.emptyTextDialog = '暂无数据';
                        that.grantDataList = [];
                        that.totalDialog = 0;
                    } else {
                        that.emptyTextDialog = '';
                        that.totalDialog = res.data.total;
                        that.grantDataList = res.data.records;
                    }

                });
            },
            getGrantPermission:function(fileId){
                var that = this;
                this.loadingGrant = true;
                ms.http.get(ms.manager + "/cms/document/permission/"+fileId+".do").then(function (res) {
                    that.loadingGrant = false;
                    if(res.code===200){
                        that.dataTreeGrant= [
                            { type: '1', name: '读取',children:res.data.read} ,
                            { type: '2', name: '写入',children:res.data.write}
                        ];
                        if(!res.data.read) that.dataTreeGrant.splice(0,1);
                        if(!res.data.write) that.dataTreeGrant.splice(1,1);
                    }

                });
            },
            //currentPage改变时会触发
            currentChangeGrant: function (currentPage) {
                this.loadingDialog = true;
                this.currentPageDialog = currentPage;
                this.listGrant();
            },
            //重置 授权管理
            resetGrant:function () {
                this.selectionGrantList = []
                this.$refs.multipleTable2.clearSelection();
                this.$refs.treeGrant.setCheckedKeys([]);
                this.searchUser="";
                this.initDialogData();
                this.listGrant();
            },
            //确认 授权管理
            grantConfirm:function () {
                var that = this;
                var treeCheckKeys = this.$refs.treeGrant.getCheckedKeys(true);
                if(this.selectionGrantList.length===0 && treeCheckKeys.length===0){
                    that.$notify({
                        type: 'warning',
                        message: "请至少选择一个用户和权限"
                    });
                }else if(this.selectionGrantList.length===0){
                    that.$notify({
                        type: 'warning',
                        message: "请至少选择一个用户"
                    });
                }else if(treeCheckKeys.length===0){
                    that.$notify({
                        type: 'warning',
                        message: "请选择权限"
                    });
                }else{
                    that.grantFrom.grantTypes = treeCheckKeys;
                    that.grantFrom.userIds = that.selectionGrantList.map(function (item) {
                        return parseInt(item.id)
                    });
                    ms.http.post(ms.manager + "/cms/document/grant.do", that.grantFrom,{
                        headers: {
                            'Content-Type': 'application/json'
                        }
                    }).then(function (res) {
                        if(res.code===200){
                            that.$notify({
                                type: 'success',
                                message: "授权成功"
                            });
                            //操作成功，刷新列表
                            that.dialogGrantVisible = false;
                            that.listGrant();
                        }else{
                            that.$notify({
                                type: 'error',
                                message: "授权失败"
                            });
                        }
                    }).catch(function (err) {
                        that.$notify({
                            type: 'error',
                            message: "授权失败"
                        });
                    });
                }
            },
            //选中用户 授权管理
            handleSelectionChangeGrant: function (val) {
                this.selectionGrantList = val;
            },
            searchUserChange:function () {
                this.initDialogData();
                this.listGrant();
            },
            initDialogData:function () {
                this.totalDialog=0;
                this.pageSizeDialog= 10;
                this.currentPageDialog= 1;
                this.loadingDialog=true;
                this.loadingGrant = false;
                this.emptyTextDialog='';
            }
        },
        created: function () {}
    });
</script>
<style>

</style>
